﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Business.Sec;

namespace ESOL.Business.ProcessingService
{
    public class SystemSec
    {
        public static bool InsertMenu(SecMenuEntity menu)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (menu.MenuTypeCode == "10"||menu.MasterFlag==0)
                    {
                        SecMenuBs.InsertMenu(menu, broker);
                    }
                    else
                    {
                        SecMenuEntity parentMenu = SecMenuBs.GetMenuByID(menu.ParentMenu);
                        if (parentMenu == null)
                        {
                            return false;
                        }
                        List<SecMenuEntity> childrenMenus = SecMenuBs.GetMenusByParentID(parentMenu.MenuId);
                        if (childrenMenus != null && childrenMenus.Count != 0)
                        {
                            foreach (SecMenuEntity entity in childrenMenus)
                            {
                                if (entity.MasterFlag == 1)
                                {
                                    entity.MasterFlag = 0;
                                    SecMenuBs.UpdateMenu(entity, broker);
                                }
                            }
                        }
                        SecMenuBs.InsertMenu(menu, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        public static bool UpdateMenu(SecMenuEntity menu)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (menu.MenuTypeCode == "10" || menu.MasterFlag==0)
                    {
                        SecMenuBs.UpdateMenu(menu,broker);
                    }
                    else
                    {
                        SecMenuEntity parentMenu = SecMenuBs.GetMenuByID(menu.ParentMenu);
                        if (parentMenu == null)
                        {
                            return false;
                        }
                        List<SecMenuEntity> childrenMenus = SecMenuBs.GetMenusByParentID(parentMenu.MenuId);
                        if (childrenMenus != null && childrenMenus.Count != 0)
                        {
                            foreach(SecMenuEntity entity in childrenMenus)
                            {
                                if (entity.MasterFlag == 1)
                                {
                                    entity.MasterFlag = 0;
                                    SecMenuBs.UpdateMenu(entity, broker);
                                }
                            }
                        }
                        SecMenuBs.UpdateMenu(menu, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch(Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }
    }
}
